Java Review We will not review the following sections: 1.2.1 Comments 1.2.2 main 1.2.3 Terminal Output (System.out.println) 1.3.1 Primitive Types 1.3.2 Constants (integer, character, and string constants, escapes) 1.3.3 Declaration and Initialization of Primitive Types 1.4.1 Assignment Operators 1.4.2 Binary Arithmetic Operators (+, -, *, /, %) 1.4.3 Unary Operators (-, ++, --) 1.4.4 Type Conversions (double) 1.5.1 Relational and Equality Operators (==, !=, <, <=, >, >=) 1.5.2 Logical Operators (&&, ||, !) 1.5.3 The if Statement 1.5.4 The while Statement 1.5.5 The for Statement 1.5.6 The do Statement 1.5.7 break and continue 1.5.8 The switch Statement 1.6 Methods (return type, name, parameters) 1.6.1 Overloading of Method Names 1.6.2 Storage Classes (static final) 2.1 Reference Variables (=, ==, type cast, ., instanceof) 2.2 Basics of Objects and References 2.2.1 The Dot Operator 2.2.2 Declarations of Objects (null, new) 2.2.4 The Meaning of = 2.2.5 Parameter Passing 2.2.6 The Meaning of == (equals) 2.3.1 Basics of String Manipulation 2.3.2 String Concatenation 2.3.3 Comparing Strings (equals, compareTo) 2.3.4 Other String Methods (length, charAt, substring) 2.3.5 Converting Other Types to Strings (toString, parseInt) 2.4 Arrays 2.4.1 Declaration, Assignment, and Methods 2.4.3 ArrayList 2.4.4 Multidimensional Arrays 1.1 Development Tools What tools are used to write Java code? editor compiler interpreter How do you use an editor to write code? emacs editor (Start -> Programs -> Editors -> Emacs) load, save syntax coloring multiple files How do you compile your code? command prompt (Start -> Programs -> Accessories -> Command Prompt) javac How do you run your code? java How do you fix your path? path=%path%;c:\jdk\bin 2.4.5 Command-line Arguments What are command-line arguments? How do you access command-line arguments in a Java program? DEMO (print the command line args) How do you compute the sum of the command-line arguments? x = Integer.parseInt(tok) DEMO (sum the command line args) What happens if you input a non-numeric argument? NumberFormatException 2.5 Exceptions How can you control what happens when an exception occurs? DEMO (add try/catch for number format) try { } catch (NumberFormatException e) { } 2.6 Input and Output How can you read numbers from a text file rather than the command line? DEMO (read from a file) import java.io.* BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); BufferedReader in = new BufferedReader( new FileReader("foobar")); line = in.readLine(); in.close(); Why does the code not compile? void method() throws IOException try { } catch (IOException e) { System.out.println(e); } How do you know you are at the end of the file? if (line == null) return; How do you handle more than one number on a line? String nums[] = line.split(" "); st = new StringTokenizer(line); st.countTokens() tok = st.nextToken() How do you write the output to a text file? DEMO (write intermediate and final sums to a file) FileWriter fw = new FileWriter("file.txt"); PrintWriter pw = new PrintWriter(fw); pw.print(); pw.println(); pw.close(); What happens if you don't close the output file? When should you cause exceptions to be thrown? check for invalid parameters passed to method How do you cause an exception to be thrown? throw new IllegalArgumentException(); DEMO (Set1.java, set bit)